/****************************************************************
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.     *
 *                                                              *
 * Copyright (C) 2009, Jun Li All rights reserved.              *
 * More about Jun{                                              *
 * Homepage: http://www.jpxx.org/                               *
 * Email   : lijun@jpxx.org                                     *
 * }                                                            *
 * SOFTWARE ENGINEERING COLLEGE OF SICHUAN UNIVERSITY           *
 *                                                              *
 ****************************************************************/
package org.jpxx.web.project;

import java.io.File;
import java.io.IOException;

import org.apache.log4j.Logger;
import org.jpxx.util.FileUtils;
import org.jpxx.util.ZipBuilder;
import org.jpxx.web.Constants;

/**
 * 给项目创建War包
 * 
 * @author Jun Li lijun@jpxx.org (http://www.jpxx.org)
 * @version 1.0.0 $ org.jpxx.web.project.ProjectWar.java $ $ Date: 2009-5-15 $
 */
public class ProjectWar {

	private Logger log = Logger.getLogger(ProjectWar.class);
	private String[] jars;
	/**
	 * WAR包生成的目录
	 */
	private String binPath = Constants.PROJECT_PATH + "bin" + File.separator;
	/**
	 * 存储直接可运行的WEB
	 */
	private String target = binPath + ".target" + File.separator;
	/**
	 * classpath
	 */
	private String classes = Constants.CLASSPATH + "classes" + File.separator;

	public ProjectWar(String[] jars) {
		this.jars = jars;
	}

	public void create() {

		log.info("Create WAR package. .........................");
		log.info(".............................................");

		File f = new File(binPath);
		if (f.exists()) {
			try {
				log.info("Clear bin directory.");
				FileUtils.delete(f.getAbsolutePath());
			} catch (IOException e) {
				log.error("Clear bin directory failed.");
			}
		} else {
			f.mkdirs();
		}

		f = new File(target);
		if (!f.exists()) {
			f.mkdirs();
		}

		try {
			log.debug("Copying file form \"" + Constants.WORKING_PATH
					+ "\" to \"" + binPath + "\" .");
			FileUtils.copy(Constants.WORKING_PATH, target);
		} catch (IOException e) {
			log.error("Copy file form \"" + Constants.WORKING_PATH + "\" to \""
					+ binPath + "\" error.");
		}

		try {
			log.debug("Copying file form \"" + classes + "\" to \"" + binPath
					+ "\" .");
			FileUtils.copy(classes, target + "WEB-INF" + File.separator
					+ "classes" + File.separator);
		} catch (IOException e) {
			log.error("Copy file form\"" + classes + "\" to \"" + binPath
					+ "\" error.");
		}

		try {
			if (jars != null)
				for (int i = 0; i < jars.length; i++) {
					FileUtils.copyFile(jars[i], target + "WEB-INF"
							+ File.separator + "lib" + File.separator);
				}
		} catch (IOException e) {
			e.printStackTrace();
		}

		log.info(".............................................");
		try {
			log.info("Create WAR package : " + target + " " + binPath
					+ Constants.PROJECT_NAME + ".war");
			ZipBuilder zb = new ZipBuilder(binPath + Constants.PROJECT_NAME
					+ ".war");
			zb.build(target);
			log.info("Create WAR package end. ........................");

		} catch (Exception e) {
			e.printStackTrace();
			log.error("Create WAR package failed. ........................");
		}

	}
}
